package com.zhxx.book.conf;

import java.nio.charset.StandardCharsets;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

/**
 * @Description:
 * @Author Alex
 * @Date 2022-11-29 10:32
 */
@Component
@Configuration
public class RestTemplateConfig {
   @Bean
   public RestTemplate restTemplate(){
      SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
      // 设置读取超时
      factory.setReadTimeout(5000);
      // 设置连接超时，单位毫秒
      factory.setConnectTimeout(5000);
      RestTemplate restTemplate = new RestTemplate(factory);
      // 支持中文编码
      restTemplate.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
      return restTemplate;
   }
}
